﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq.Expressions;
using Newtonsoft.Json.Linq;
using WebGrease.Css.Extensions;

using CanDoo;
using CanDoo.Core;
using CanDoo.Data;
using CanDoo.Web;
using CanDoo.FineUI;
using CanDoo.Extensions;
using CanDoo.Oil.Core.Models;
using CanDoo.Oil.Core.Contracts;
using CanDoo.Admin.Core.Contracts;
using CanDoo.Core.Data;
using FineUIPro;

namespace CanDoo.Oil.WebApp.storeOilSerialNum
{
    public partial class InBuyBack : ListPageBase<IOilSerialNumContract, OilSerialNum>
    {


        public IInStoreTypeContract InStoreTypeContract { get; set; }
        public IOutStoreTypeContract OutStoreTypeContract { get; set; }
        public IWarehouseContract WarehouseContract { get; set; }
        public IProductContract ProductContract { get; set; }
        public ICustomerContract CustomerContract { get; set; }
        public IOilSalesSerialNumContract OilSalesSerialNumContract { get; set; }

        public long WarehouseId = Web.Request.QueryString("WareHouseId").ToLong();
        public long EntityId = Web.Request.QueryString("EntityId").ToLong();
        public long ProductId = Web.Request.QueryString("ProductId").ToLong();

        #region 方法
        protected override void InitGrid()
        {
            Selector = p => new
            {
                p.Warehouse,
                p.WarehouseId,
                p.Product,
                p.ProductId,
                p.SerialNumber,
                p.Id,
                p.Name,
            };
            base.InitGrid();
        }

        protected override void Search()
        {
            var key = SearchKey.GetValue();
            if (string.IsNullOrEmpty(key) == false)
            {
                Filter = Filter.And(p => p.SerialNumber.Contains(key));
            }
            Filter = Filter.And(p => p.WarehouseId == WarehouseId);
            Filter = Filter.And(p => p.ProductId == ProductId);
            Filter = Filter.And(p => p.InBuyBackId == EntityId);
        }
        #endregion

        #region 事件
        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitGrid();
            }
        }
        #endregion

        protected override void btnDelete_Click(object sender, EventArgs e)
        {
            if (!EnableDelete)
            {
                ShowLimitError.CheckPowerFailWithAlert();
                return;
            }
            var ids = _GridList.GetSelectedDataKeyIDs();
            if (ids.Count == 0)
            {
                Alert.ShowInTop("请至少选择一项进行删除");
                return;
            }
            OperationResult result;
            try
            {
                result = Manager.DeleteFromInBuyBackByIds(ids.ToArray());
            }
            catch (Exception ex)
            {
                result = new OperationResult(OperationResultType.Error, ex.Message);
            }

                OperateLoger.OperationResult = result;
                OperateLoger.SaveLogAsync();

            Alert.ShowInTop(result.Message);
            _GridList.SelectedRowIndexArray = new int[0];
            InitGrid();
        }

        protected override void GridList_RowCommand(object sender, GridCommandEventArgs e)
        {
            var keys = _GridList.DataKeys[e.RowIndex];
            var id = keys[0].ToString().ToLong();
            switch (e.CommandName.ToLower())
            {
                case StaticConfig.COMMANDNAME_DELETE:
                    if (!EnableDelete)
                    {
                        ShowLimitError.CheckPowerFailWithAlert();
                        return;
                    }
                    var oR = Manager.DeleteFromInBuyBack(id);
                    Alert.ShowInTop(oR.Message);

                    //Logger.Info(this.PageTitle, this.Request.Url.ToString() + string.Format("更新数据:id={0}", id), "Delete");

                    _GridList.SelectedRowIndexArray = new int[0];
                    InitGrid();
                    break;
                default:
                    RowCommand(e.CommandName, keys);
                    break;
            }
        }
        #endregion 事件

    }
}
